package com.dream.contacts;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;

import android.content.Context;
import android.database.Cursor;
import android.provider.ContactsContract.Contacts;

public class SmartDial {
	
	private static final String[] CONTACT_PROJECTION = new String[] {
		Contacts._ID, Contacts.DISPLAY_NAME, Contacts.TIMES_CONTACTED };
	
	private static final String CONTACT_QUERY = Contacts.HAS_PHONE_NUMBER
			+ " > 0";
	
	private static final String CONTACT_SORT = Contacts._ID + " ASC";
	
	Cursor contact = null;
	
	private static final String FIRST_PINYIN_UNIHAN =  "\u5416";
    private static final String LAST_PINYIN_UNIHAN =  "\u5497";
    
    static HashMap dialerKeyMap;
    
    static {
		HashMap dialerKeyMap = new HashMap<Character, Character>();
		dialerKeyMap.put('0', '0');
		dialerKeyMap.put('1', '1');
		dialerKeyMap.put('2', '2');
		dialerKeyMap.put('A', '2');
		dialerKeyMap.put('B', '2');
		dialerKeyMap.put('C', '2');
		dialerKeyMap.put('3', '3');
		dialerKeyMap.put('D', '3');
		dialerKeyMap.put('E', '3');
		dialerKeyMap.put('F', '3');
		dialerKeyMap.put('4', '4');
		dialerKeyMap.put('G', '4');
		dialerKeyMap.put('H', '4');
		dialerKeyMap.put('I', '4');
		dialerKeyMap.put('5', '5');
		dialerKeyMap.put('J', '5');
		dialerKeyMap.put('K', '5');
		dialerKeyMap.put('L', '5');
		dialerKeyMap.put('6', '6');
		dialerKeyMap.put('M', '6');
		dialerKeyMap.put('N', '6');
		dialerKeyMap.put('O', '6');
		dialerKeyMap.put('7', '7');
		dialerKeyMap.put('P', '7');
		dialerKeyMap.put('Q', '7');
		dialerKeyMap.put('R', '7');
		dialerKeyMap.put('S', '7');
		dialerKeyMap.put('8', '8');
		dialerKeyMap.put('T', '8');
		dialerKeyMap.put('U', '8');
		dialerKeyMap.put('V', '8');
		dialerKeyMap.put('9', '9');
		dialerKeyMap.put('W', '9');
		dialerKeyMap.put('X', '9');
		dialerKeyMap.put('Y', '9');
		dialerKeyMap.put('Z', '9');
	}

	public SmartDial(Context mContext){
		contact = mContext.getContentResolver().query(
				Contacts.CONTENT_URI, CONTACT_PROJECTION, CONTACT_QUERY, null,
				CONTACT_SORT);
	}
	
	public static String getPinyin(String chineseName){
		String sss = ContactLocaleUtils.getIntance().getSortKey(chineseName, FullNameStyle.CHINESE);
		
		return spitFCKeyMap(sss);
	}
	
	
	public static String spitQuanPinKeyMap(String sss){
		StringTokenizer token = new StringTokenizer(sss, " ");
		StringBuffer bf =new StringBuffer();
		ArrayList<String> partNames = new ArrayList<String>();
		while(token.hasMoreElements()){
			partNames.add(token.nextToken());
		}
		if((partNames.size() % 2)!= 0){
			return partNames.get(0);
		}else{
			for(int i= 0;i< partNames.size(); i++){
				if(i%2 == 0)
				bf.append(dialerKeyMap.get(partNames.get(i)));
			}
		}
		
		return bf.toString();
	}
	
	public static String spitQuanPin(String sss){
		StringTokenizer token = new StringTokenizer(sss, " ");
		StringBuffer bf =new StringBuffer();
		ArrayList<String> partNames = new ArrayList<String>();
		while(token.hasMoreElements()){
			partNames.add(token.nextToken());
		}
		if((partNames.size() % 2)!= 0){
			return partNames.get(0);
		}else{
			for(int i= 0;i< partNames.size(); i++){
				if(i%2 == 0)
				bf.append(partNames.get(i));
			}
		}
		
		return bf.toString();
	}
	
	public static String spitFCKeyMap(String sss){
		StringTokenizer token = new StringTokenizer(sss, " ");
		StringBuffer bf =new StringBuffer();
		ArrayList<String> partNames = new ArrayList<String>();
		while(token.hasMoreElements()){
			partNames.add(token.nextToken());
		}
		if((partNames.size() % 2)!= 0){
			return partNames.get(0);
		}else{
			for(int i= 0;i< partNames.size(); i++){
				if(i%2 == 0)
				bf.append(dialerKeyMap.get(partNames.get(i).substring(0, 1)));
			}
		}
		
		return bf.toString();
	}
	
	public static String spitFC(String sss){
		StringTokenizer token = new StringTokenizer(sss, " ");
		StringBuffer bf =new StringBuffer();
		ArrayList<String> partNames = new ArrayList<String>();
		while(token.hasMoreElements()){
			partNames.add(token.nextToken());
		}
		if((partNames.size() % 2)!= 0){
			return partNames.get(0);
		}else{
			for(int i= 0;i< partNames.size(); i++){
				if(i%2 == 0)
				bf.append(partNames.get(i).substring(0, 1));
			}
		}
		
		return bf.toString();
	}
	
	
}
